package com.zjw.zy.heart.bean.response;

import jakarta.annotation.Nullable;
import lombok.Data;

import java.io.Serializable;

@Data
public class BaseResponse<RESULT> implements Serializable {

    public static final int CODE_SUCCESS = 200;
    public static final int CODE_FAILED = 201;

    private Integer code;
    private String message;
    @Nullable
    private RESULT result;


    public static <T> BaseResponse<T> ofSuccess(@Nullable T result) {
        BaseResponse<T> response = new BaseResponse<>();
        response.code = CODE_SUCCESS;
        response.message = "请求成功";
        response.result = result;
        return response;
    }


    public static <T> BaseResponse<T> ofFailed(@Nullable T result) {
        BaseResponse<T> response = new BaseResponse<>();
        response.code = CODE_FAILED;
        response.message = "请求失败";
        response.result = result;
        return response;
    }

    public static <T> BaseResponse<T> of(T result) {
        if (result == null) {
            return ofFailed(null);
        }

        return ofSuccess(result);
    }
}
